<?php
class CategoryModel extends Model {
	var $tableName = 'course_category';
	
	// 添加分类
	function addCategory($name, $pid = '') {
		if (empty ( $name )) {
			return false;
		}
		
		// 如何分类已经存在，不作处理
		$isExist = $this->where ( array (
				'name' => $name 
		) )->getField ( 'id' );
		if ($isExist) {
			return false;
		}
		
		// 如果有父级ID，取出父级信息
		if (! empty ( $pid )) {
			$map ['id'] = intval ( $pid );
			$parent = $this->where ( $map )->getField ( 'pidList' );
			
			$save ['pid'] = $pid;
			if (! empty ( $parent )) {
				$save ['pidList'] = $parent . ',' . $pid;
			} else {
				$save ['pidList'] = $pid;
			}
		}
		
		$save ['name'] = $name;
		$res = $this->add ( $save );
		
		return $res;
	}
	
	// 修改分类
	function editCategory($name, $id) {
		if (empty ( $name ) || empty ( $id )) {
			return false;
		}
		
		$save ['name'] = $name;
		$res = $this->where ( array (
				'id' => $id 
		) )->save ( $save );
		
		return $res;
	}
	
	// 刪除分类
	function deleteCategory($id) {
		if (empty ( $id )) {
			return false;
		}
		
		$res = $this->where ( array (
				'id' => $id 
		) )->delete ();
		
		return $res;
	}
}
?>